package com.example.sell.common;

import com.example.sell.enums.ResultVO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;

import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 *
 * @author ouyangzhixiang
 * @date 2018/6/26
 */
@Getter
public class ResultVOUtil<T> implements Serializable {
    private Integer status;
    private String msg;
    private T data;

    private ResultVOUtil(Integer status) {
        this.status = status;
    }

    private ResultVOUtil(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    private ResultVOUtil(Integer status, T data) {
        this.status = status;
        this.data = data;
    }

    private ResultVOUtil(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public boolean isSuccess() {
        return this.status.equals( ResultVO.SUCCESS.getCode());
    }

    public static <T> ResultVOUtil<T> createBySuccessMessage(String msg) {
        return new ResultVOUtil<T>(ResultVO.SUCCESS.getCode(), msg);
    }

    @JsonIgnore
    public static <T> ResultVOUtil<T> createBySuccess(T data) {
        return new ResultVOUtil<T>(ResultVO.SUCCESS.getCode(), data);
    }

    public static <T> ResultVOUtil<T> createBySuccessMessage(String msg, T data) {
        return new ResultVOUtil<T>(ResultVO.SUCCESS.getCode(), msg, data);
    }

    public static <T> ResultVOUtil<T> createByError() {
        return new ResultVOUtil<T>(ResultVO.ERROR.getCode(), ResultVO.ERROR.getDesc());
    }

    public static <T> ResultVOUtil<T> createByErrorMessage(String errorMessage) {
        return new ResultVOUtil<T>(ResultVO.ERROR.getCode(), errorMessage);
    }

    public static <T> ResultVOUtil<T> createByErroCodeMessage(Integer errorCode, String errorMessage) {
        return new ResultVOUtil<T>(errorCode, errorMessage);
    }
}
